package com.me.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.me.constant.Constant;
import com.me.enums.ScreenEnum;
import com.me.happybugs.HappyBugs_Main;
import com.me.helper.PreferencesData;
import com.me.utils.ScreenshotFactory;

public class BestScoreScreen extends BaseScreen {

	private Image imgBackGround, img_bgTop;
	private ImageButton img_back;
	private Label lb_BestScore, lb_currentScore, lb_Score, lb_title;
	private TextButton btn_shareGame;

	@Override
	public void show() {
		super.show();

		BaseScreen.stage.clear();

		imgBackGround = new Image(HappyBugs_Main.getAtlas().findRegion(
				"datagame/woodBG"));
		stage.addActor(imgBackGround);

		lb_BestScore = new Label("Best score:   "
				+ PreferencesData.getHighScore(1), Constant.skin, "labelMenu");
		lb_BestScore.setX(280);
		lb_BestScore.setY(450);

		lb_currentScore = new Label("Your current score:  " + Constant.iScore,
				Constant.skin, "labelMenuScore");
		lb_currentScore.setX(280);
		lb_currentScore.setY(350);

		lb_Score = new Label("Score", Constant.skin, "labelMenuScore");
		lb_Score.setX(1000);
		lb_Score.setY(550);

		img_bgTop = new Image(
				Constant.atlas.findRegion("datagame/menu-bottom-thanh-ngang"));
		img_bgTop.setBounds(0, Constant.STAGE_HEIGHT - 70,
				Constant.STAGE_WIDTH, 70);
		stage.addActor(img_bgTop);

		lb_title = new Label("--HAPPY BUGS--", Constant.skin, "labelMenu");
		lb_title.setX(Constant.STAGE_WIDTH / 2 - lb_title.getWidth() / 2);
		lb_title.setY(Constant.STAGE_HEIGHT - img_bgTop.getHeight()
				+ (img_bgTop.getHeight() - lb_title.getHeight()) / 2);
		stage.addActor(lb_title);

		TextureRegionDrawable bup = new TextureRegionDrawable(
				Constant.atlas.findRegion("datagame/Back_up"));
		TextureRegionDrawable bdown = new TextureRegionDrawable(
				Constant.atlas.findRegion("datagame/Back_down"));
		img_back = new ImageButton(bup, bdown);
		img_back.setX(20);
		img_back.setY(Constant.STAGE_HEIGHT - img_bgTop.getHeight()
				+ (img_bgTop.getHeight() - img_back.getHeight()) / 2);
		img_back.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				Constant.isSharedScore = false;
				if (Constant.b_bestScoreMain) {
					HappyBugs_Main.getScreenManager().createScreen(
							ScreenEnum.MENU_SCREEN);
					HappyBugs_Main.getScreenManager().getScreen().show();
				} else {
					HappyBugs_Main.getScreenManager().createScreen(
							ScreenEnum.END_GAME);
					HappyBugs_Main.getScreenManager().getScreen().show();
				}
			}
		});

		btn_shareGame = new TextButton("Share score", Constant.skin,
				"textButton_45_bold");
		btn_shareGame.setX(440);
		btn_shareGame.setY(200);
		btn_shareGame.setWidth(450);
		btn_shareGame.setHeight(100);
		btn_shareGame.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Gdx.app.postRunnable(new Runnable() {
					@Override
					public void run() {
						if (!Constant.isSharedScore) {
							HappyBugs_Main.getApi().showToast("Please wait!");
							if (!Gdx.files.getExternalStoragePath().isEmpty()) {
								ScreenshotFactory.saveScreenshot();
								String str = Gdx.files.getExternalStoragePath()
										+ "/score.png";
								HappyBugs_Main.getApi().shareImage(str);
							} else {
								HappyBugs_Main.getApi().showToast(
										"Sd card not found");
							}
							Constant.isSharedScore = true;
						} else {
							HappyBugs_Main.getApi().showToast(
									"Back to menu, plz");
						}
					}
				});

			}

		});

		if (Constant.iScore > PreferencesData.getHighScore(1)) {
			PreferencesData.setHighScore(Constant.iScore, 1);
			Constant.resetGame();
		}

		stage.addActor(img_back);
		stage.addActor(lb_currentScore);
		stage.addActor(lb_BestScore);
		stage.addActor(btn_shareGame);
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		camera.update();
		sb.setProjectionMatrix(camera.combined);

		if (Gdx.input.isKeyPressed(Keys.BACK)) {
			HappyBugs_Main.getScreenManager().createScreen(
					ScreenEnum.MENU_SCREEN);
			HappyBugs_Main.getScreenManager().getScreen().show();
		}
		stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT);
	}
}
